/**
 * 
 */
package co.gov.medellin.sdep.sisdep.cco.service;

import java.util.Date;
import java.util.List;

import co.gov.medellin.sdep.sisdep.cco.exception.AgendaException;
import co.gov.medellin.sdep.sisdep.cco.exception.AgendaRuntimeException;
import co.gov.medellin.sdep.sisdep.cco.exception.CentroComercialException;
import co.gov.medellin.sdep.sisdep.cco.exception.CentroComercialRuntimeException;
import co.gov.medellin.sdep.sisdep.cco.exception.descriptor.AgendaMessageDescriptor;
import co.gov.medellin.sdep.sisdep.cco.exception.descriptor.CentroComercialMessageDescriptor;
import co.gov.medellin.sdep.sisdep.common.message.MessageDescriptorConstant;
import co.gov.medellin.sdep.sisdep.common.service.BaseService;
import co.gov.medellin.sdep.sisdep.model.Agenda;
import co.gov.medellin.sdep.sisdep.model.dao.impl.IAgendaDAOT;

/**
 * @author Usuario
 *
 */
public class AgendaServiceImpl extends BaseService implements IAgendaService {

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                 init()
	//----------------------------------------------------------------------

	/**
	 * default serial version
	 */
	private static final long serialVersionUID = 1L;
	/** */
	private IAgendaDAOT agendaDAO;

	/**
	 * 
	 */
	public AgendaServiceImpl() {
	}

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                  end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                     init()
	//----------------------------------------------------------------------

	/**
	 * 
	 */
	public void insert(Agenda agenda) throws AgendaException {
		try{
			agenda.setUsuarioCrea(getPrincipalUser());
			agenda.setFechaCrea(new Date());
			getAgendaDAO().insert(agenda);
		}catch (Exception e) {
			Object [] infoError = {
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new AgendaException(new AgendaMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new AgendaException(new AgendaMessageDescriptor(MessageDescriptorConstant.SAVE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void update(Agenda agenda) throws AgendaException {
		try{
			agenda.setUsuarioModifica(getPrincipalUser());
			agenda.setFechaModifica(new Date());
			getAgendaDAO().update(agenda);
		}catch (Exception e) {
			Object [] infoError = {
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new AgendaRuntimeException(new AgendaMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new AgendaRuntimeException(new AgendaMessageDescriptor(MessageDescriptorConstant.UPDATE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void delete(Agenda agenda) throws AgendaException {
		try {
			agenda.setUsuarioModifica(getPrincipalUser());
			agenda.setFechaModifica(new Date());
			getAgendaDAO().delete(agenda);
		}catch (Exception e) {
			Object [] infoError = {
			};
			throw new AgendaRuntimeException(new AgendaMessageDescriptor(MessageDescriptorConstant.DELETE), infoError, e);
		}
	}

	/**
	 * @return 
	 * 
	 */
	public final List load(int start,int max, Agenda agenda) throws AgendaException {
		try{
			//return getAgendaDAO().getAllAgendaPaginated(start, max, criteria);
			return getAgendaDAO().load(agenda);
		}catch (Exception e) {
			Object [] infoError = null;
			throw new AgendaRuntimeException(new AgendaMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}

	/**
	 * 
	 */
	public final Agenda get(long id) throws AgendaException {
		try{
			return getAgendaDAO().findByPrimaryKey(id);
		}catch (Exception e) {
			Object [] infoError = {new Long(id)};
			throw new AgendaRuntimeException(new AgendaMessageDescriptor(MessageDescriptorConstant.GET), infoError, e);
		}
	}

	public List generatePDFAgenda(Agenda agenda) throws AgendaException {
		try{
			return getAgendaDAO().generatePDFAgenda(agenda);
		} catch (Exception e) {
			Object [] infoError = null;
			
			throw new CentroComercialRuntimeException(
					new CentroComercialMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}	
	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                      end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- getters and setters                                         init()
	//----------------------------------------------------------------------

	/**
	 * @author Usuario
	 *
	 */
	public IAgendaDAOT getAgendaDAO() {
		return agendaDAO;
	}

	/**
	 * @author Usuario
	 *
	 */
	public void setAgendaDAO(IAgendaDAOT agendaDAO) {
		this.agendaDAO = agendaDAO;
	}

	//----------------------------------------------------------------------
	//--- getters and setters                                          end()
	//----------------------------------------------------------------------

}
